using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class HoaDon_DAL
    {
        private SqlConnection cn = null;
        public  HoaDon_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public int Insert(HoaDon_DTO hoaDon)
        {
            SqlCommand cm = new SqlCommand("tb_HoaDon_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            //cm.Parameters.AddWithValue("@MaNV", hoaDon.MaNV);
            cm.Parameters.AddWithValue("@MaKH", hoaDon.MaKH);
            cm.Parameters.AddWithValue("@MaHinhThuc_ThanhToan", hoaDon.MaHinhThuc_ThanhToan);
            cm.Parameters.AddWithValue("@MaTinhTrang_DH", hoaDon.MaTinhTrang_DH);
            cm.Parameters.AddWithValue("@NgayDatHang", hoaDon.NgayDatHang);
            cm.Parameters.AddWithValue("@NgayGiaoHang", hoaDon.NgayGiaoHang);
            cm.Parameters.AddWithValue("@Diachigiao", hoaDon.DiaChiGiao);
            //cm.Parameters.AddWithValue("@NgayGioHuy_DonHang", hoaDon.NgayGioHuy_DonHang);
            cm.Parameters.AddWithValue("@TongTien", hoaDon.TongTien);
            return int.Parse(cm.ExecuteScalar().ToString());
       
        }
        public void Update(HoaDon_DTO hoaDon)
        {
            SqlCommand cm = new SqlCommand("tb_HOADON_UPDATE", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHD", hoaDon.MaHD);
            //cm.Parameters.AddWithValue("@MaNV", hoaDon.MaNV);
            //cm.Parameters.AddWithValue("@MaKH", hoaDon.MaKH);
            //cm.Parameters.AddWithValue("@MaHinhThuc_ThanhToan", hoaDon.MaHinhThuc_ThanhToan);
            cm.Parameters.AddWithValue("@MaTinhTrang_DH", hoaDon.MaTinhTrang_DH);
            //cm.Parameters.AddWithValue("@NgayDatHang", hoaDon.NgayDatHang);
            //cm.Parameters.AddWithValue("@NgayGiaoHang", hoaDon.NgayGiaoHang);
            ////cm.Parameters.AddWithValue("@NgayGioHuy_DonHang", hoaDon.NgayGioHuy_DonHang);
            //cm.Parameters.AddWithValue("@TongTien", hoaDon.TongTien);
            cm.ExecuteNonQuery();
        }
        public void UpdateTONGTIEN(HoaDon_DTO hoaDonTT)
        {


            SqlCommand cm = new SqlCommand("hoaDon_capnhat_tongtien", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHD", hoaDonTT.MaHD);
            //cm.Parameters.AddWithValue("@MaNV", hoaDon.MaNV);
            //cm.Parameters.AddWithValue("@MaKH", hoaDon.MaKH);
            //cm.Parameters.AddWithValue("@MaHinhThuc_ThanhToan", hoaDon.MaHinhThuc_ThanhToan);
            cm.Parameters.AddWithValue("@TONGTIEN", hoaDonTT.TongTien);
            //cm.Parameters.AddWithValue("@NgayDatHang", hoaDon.NgayDatHang);
            //cm.Parameters.AddWithValue("@NgayGiaoHang", hoaDon.NgayGiaoHang);
            ////cm.Parameters.AddWithValue("@NgayGioHuy_DonHang", hoaDon.NgayGioHuy_DonHang);
            //cm.Parameters.AddWithValue("@TongTien", hoaDon.TongTien);
            cm.ExecuteNonQuery();
        }
        
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_HoaDon_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHD", iD);
            cm.ExecuteNonQuery();
        }
        public tbHoaDonCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_HoaDon_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbHoaDonCollection hoaDonCollection = new tbHoaDonCollection();

            while (dr.Read())
            {
                HoaDon_DTO hoaDon = new HoaDon_DTO();
                hoaDon.MaHD = int.Parse(dr.GetValue(0).ToString());
                //hoaDon.MaNV = int.Parse(dr.GetValue(1).ToString());
                hoaDon.MaKH = int.Parse(dr.GetValue(1).ToString());
                hoaDon.MaHinhThuc_ThanhToan = int.Parse(dr.GetValue(2).ToString());
                hoaDon.MaTinhTrang_DH = int.Parse(dr.GetValue(3).ToString());
                hoaDon.NgayDatHang = DateTime.Parse(dr.GetValue(4).ToString());
                hoaDon.NgayGiaoHang = DateTime.Parse(dr.GetValue(5).ToString());
                //hoaDon.NgayGioHuy_DonHang = DateTime.Parse(dr.GetValue(7).ToString());
                hoaDon.DiaChiGiao = dr.GetValue(6).ToString();
                hoaDon.TongTien = float.Parse(dr.GetValue(7).ToString());

                hoaDonCollection.Add(hoaDon);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hoaDonCollection;
        }
    }
}
